﻿using System.Web;

using Autofac;
using Domain.Uow;
using Autofac.Integration.Mvc;
using Infrastructure.Data.Uow;
using Infrastructure.Crosscutting.NetFramework;
using Infrastructure.Crosscutting.NetFramework.Fakes;

namespace Presentation.Web.Admin
{
    public class PreAdminIoc
    {
        public static void Register(Autofac.ContainerBuilder builder)
        {
            builder.RegisterControllers(System.Reflection.Assembly.GetExecutingAssembly());
            Repository(builder);
            Crosscutting(builder);
            Authorize(builder);
            CommService(builder);
            NetFramework(builder);
            DataUow(builder);
            AppService(builder);
            SQLiteProvider(builder);
            ClientSQLite(builder);
        }

        private static void Repository(Autofac.ContainerBuilder builder)
        {
            #region repository

            builder.RegisterGeneric(typeof(EfRepository<>)).As(typeof(IRepository<>)).InstancePerHttpRequest();

            #endregion
        }

        private static void Crosscutting(Autofac.ContainerBuilder builder)
        {
            #region crosscutting

            builder.RegisterType<Infrastructure.Crosscutting.Caches.CacheService>()
               .As<Infrastructure.Crosscutting.Caches.ICacheService>()
               .SingleInstance();

            #endregion
        }

        private static void Authorize(Autofac.ContainerBuilder builder)
        {
            #region authorize

            builder.RegisterType<Infrastructure.Crosscutting.Authorize.MvcActionService>()
                .As<Infrastructure.Crosscutting.Authorize.IMvcActionService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Infrastructure.Crosscutting.Authorize.MvcControllerService>()
                .As<Infrastructure.Crosscutting.Authorize.IMvcControllerService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Infrastructure.Crosscutting.Authorize.RoleService>()
                .As<Infrastructure.Crosscutting.Authorize.IRoleService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Infrastructure.Crosscutting.Authorize.UserRoleService>()
                .As<Infrastructure.Crosscutting.Authorize.IUserRoleService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Infrastructure.Crosscutting.Authorize.UserService>()
                .As<Infrastructure.Crosscutting.Authorize.IUserService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Infrastructure.Crosscutting.Authorize.MvcControllerClassService>()
                .As<Infrastructure.Crosscutting.Authorize.IMvcControllerClassService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Infrastructure.Crosscutting.Authorize.MvcRolePermissionService>()
                .As<Infrastructure.Crosscutting.Authorize.IMvcRolePermissionService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Infrastructure.Crosscutting.Authorize.InitPermissionService>()
                .As<Infrastructure.Crosscutting.Authorize.IInitPermissionService>()
                .InstancePerHttpRequest();

            //add by yjihrp 2012.4.19.12.48
            builder.RegisterType<Infrastructure.Crosscutting.Authorize.MvcActionClassService>()
                .As<Infrastructure.Crosscutting.Authorize.IMvcActionClassService>()
                .InstancePerHttpRequest();
            //end add by yjihrp 2012.4.19.12.48

            builder.RegisterType<Infrastructure.Crosscutting.Authorize.AssemblyPermissionService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Infrastructure.Crosscutting.Authorize.RoleMenuService>()
                .As<Infrastructure.Crosscutting.Authorize.IRoleMenuService>()
                .InstancePerHttpRequest();

            #endregion
        }

        private static void CommService(Autofac.ContainerBuilder builder)
        {
            #region commService
            #endregion
        }

        private static void NetFramework(Autofac.ContainerBuilder builder)
        {
            //这个里面再写入各个层的入注就行了，
            #region register httpcontext
            //register FakeHttpContext when HttpContext is not available
            builder.Register(c => HttpContext.Current != null ? (new HttpContextWrapper(HttpContext.Current) as HttpContextBase) : (new FakeHttpContext("~/") as HttpContextBase))
                .As<HttpContextBase>()
                .InstancePerHttpRequest();

            builder.Register(c => c.Resolve<HttpContextBase>().Request)
                .As<HttpRequestBase>()
                .InstancePerHttpRequest();
            builder.Register(c => c.Resolve<HttpContextBase>().Response)
                .As<HttpResponseBase>()
                .InstancePerHttpRequest();
            builder.Register(c => c.Resolve<HttpContextBase>().Server)
                .As<HttpServerUtilityBase>()
                .InstancePerHttpRequest();
            builder.Register(c => c.Resolve<HttpContextBase>().Session)
                .As<HttpSessionStateBase>()
                .InstancePerHttpRequest();

            builder.RegisterType<WebWorkContext>().As<IWorkContext>()
                .InstancePerHttpRequest();
            #endregion

        }

        private static void DataUow(Autofac.ContainerBuilder builder)
        {
            #region dataUow
            builder.RegisterType<Infrastructure.Data.Uow.UnitOfWorkContext>()
            .As<Infrastructure.Data.Uow.UnitOfWorkIQueryable>().InstancePerHttpRequest();
            #endregion
        }

        private static void AppService(Autofac.ContainerBuilder builder)
        {
            #region advertise

            builder.RegisterType<Application.Admin.Advertise.AdvertisePicService>()
               .As<Application.Admin.Advertise.IAdvertisePicService>().InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Advertise.AdvertiseService>()
               .As<Application.Admin.Advertise.IAdvertiseService>().InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Advertise.OpenAdvertiseService>()
               .As<Application.Admin.Advertise.IOpenAdvertiseService>().InstancePerHttpRequest();

            #endregion

            #region article

            builder.RegisterType<Application.Admin.Article.ArticleClassService>()
                .As<Application.Admin.Article.IArticleClassService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Article.ArticleCommService>()
                .As<Application.Admin.Article.IArticleCommService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Article.ArticleInfoClassService>()
                .As<Application.Admin.Article.IArticleInfoClassService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Article.ArticlePicService>()
                .As<Application.Admin.Article.IArticlePicService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Article.ArticleService>()
                .As<Application.Admin.Article.IArticleService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Article.ArticleTagService>()
                .As<Application.Admin.Article.IArticleTagService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Article.OpenArticleService>()
                .As<Application.Admin.Article.IOpenArticleService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Article.ArticleSightService>()
                .As<Application.Admin.Article.IArticleSightService>()
                .InstancePerHttpRequest();

            #endregion

            #region pic

            builder.RegisterType<Application.Admin.Pic.PicClassService>()
                .As<Application.Admin.Pic.IPicClassService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Pic.PicCommService>()
                .As<Application.Admin.Pic.IPicCommService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Pic.PicInfoClassService>()
                .As<Application.Admin.Pic.IPicInfoClassService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Pic.PicInfoService>()
                .As<Application.Admin.Pic.IPicInfoService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Pic.PicTagService>()
                .As<Application.Admin.Pic.IPicTagService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Pic.PicThumbService>()
                .As<Application.Admin.Pic.IPicThumbService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Pic.PicThumbSizeService>()
                .As<Application.Admin.Pic.IPicThumbSizeService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Pic.OpenPicService>()
              .As<Application.Admin.Pic.IOpenPicService>()
              .InstancePerHttpRequest();


            #endregion

            #region sight

            builder.RegisterType<Application.Admin.Sight.SightPointAreaService>()
                .As<Application.Admin.Sight.ISightPointAreaService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Sight.SightPointClassService>()
                .As<Application.Admin.Sight.ISightPointClassService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Sight.SightPointService>()
                .As<Application.Admin.Sight.ISightPointService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Sight.SightPointGoodsService>()
                .As<Application.Admin.Sight.ISightPointGoodsService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Sight.SightPointImportLogService>()
                .As<Application.Admin.Sight.ISightPointImportLogService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Sight.SightPointPicService>()
                .As<Application.Admin.Sight.ISightPointPicService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Sight.SightPointCommReplyService>()
                .As<Application.Admin.Sight.ISightPointCommReplyService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Sight.SightPointCommService>()
                .As<Application.Admin.Sight.ISightPointCommService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Sight.SightPointUserSiginService>()
                .As<Application.Admin.Sight.ISightPointUserSiginService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Sight.OpenSightService>()
                .As<Application.Admin.Sight.IOpenSightService>()
                .InstancePerHttpRequest();

            #endregion

            #region goods

            builder.RegisterType<Application.Admin.Goods.GoodsService>()
                .As<Application.Admin.Goods.IGoodsService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Goods.GoodsPicService>()
                .As<Application.Admin.Goods.IGoodsPicService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Goods.GoodsImportLogService>()
                .As<Application.Admin.Goods.IGoodsImportLogService>()
                .InstancePerHttpRequest();

            #endregion

            #region tour

            builder.RegisterType<Application.Admin.Tour.TourService>()
                .As<Application.Admin.Tour.ITourService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Tour.TourPicService>()
                .As<Application.Admin.Tour.ITourPicService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Tour.TourImportLogService>()
                .As<Application.Admin.Tour.ITourImportLogService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Tour.TourClassService>()
                .As<Application.Admin.Tour.ITourClassService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Tour.TourSightService>()
                .As<Application.Admin.Tour.ITourSightService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Tour.OpenTourService>()
                .As<Application.Admin.Tour.IOpenTourService>()
                .InstancePerHttpRequest();

            #endregion

            #region schedule

            builder.RegisterType<Application.Admin.Schedule.ScheduleService>()
                .As<Application.Admin.Schedule.IScheduleService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Schedule.ScheduleImportLogService>()
                .As<Application.Admin.Schedule.IScheduleImportLogService>()
                .InstancePerHttpRequest();

            #endregion

            #region export import

            builder.RegisterType<Application.Admin.ExportImport.ExportService>()
                .As<Application.Admin.ExportImport.IExportService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.ExportImport.ImportService>()
                .As<Application.Admin.ExportImport.IImportService>()
                .InstancePerHttpRequest();

            #endregion

            #region mobile client

            builder.RegisterType<Application.Admin.MobileClient.MobileClientService>()
                .As<Application.Admin.MobileClient.IMobileClientService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.MobileClient.MobileClientStartLogService>()
                .As<Application.Admin.MobileClient.IMobileClientStartLogService>()
                .InstancePerHttpRequest();

            #endregion

            #region push

            builder.RegisterType<Application.Admin.Push.PushContentsService>()
                .As<Application.Admin.Push.IPushContentsService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Push.PushLogService>()
                .As<Application.Admin.Push.IPushLogService>()
                .InstancePerHttpRequest();

            builder.RegisterType<Application.Admin.Push.AutoPushDetailService>()
                .As<Application.Admin.Push.IAutoPushDetailService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Push.AutoPushService>()
                .As<Application.Admin.Push.IAutoPushService>()
                .InstancePerHttpRequest();
            builder.RegisterType<Application.Admin.Push.AutoPushTimerService>()
                .As<Application.Admin.Push.IAutoPushTimerService>()
                .InstancePerHttpRequest();

            #endregion

            #region

            builder.RegisterType<Application.Admin.Report.ReportService>()
                .As<Application.Admin.Report.IReportService>()
                .InstancePerHttpRequest();

            #endregion
        }

        private static void SQLiteProvider(Autofac.ContainerBuilder builder)
        {
            builder.RegisterType<System.Data.SQLiteProvider.SQLiteConfigService>().InstancePerHttpRequest();
            builder.Register(c => new System.Data.SQLiteProvider
                .SQLiteSqlService(c.Resolve<System.Data.SQLiteProvider.SQLiteConfigService>().ConnectionStringBuilder
                )
            );
        }

        private static void ClientSQLite(Autofac.ContainerBuilder builder)
        {
            builder.RegisterType<Infrastructure.Data.Client.SQLite.ClientDbService>().InstancePerHttpRequest();
        }
    }
}